package com.xhsj.user.pms.overseas.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhsj.user.pms.overseas.entity.TbPamOverseasCountryDict;
import com.xhsj.user.pms.overseas.service.TbPamOverseasCountryDictService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.RestController;




/**
 * <p>
 * 专利海外国家字典 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Jun 1, 2021 8:22:26 AM
 */
@Slf4j
@Api(tags = "专利海外国家字典")
@RestController
@RequestMapping("/patent/tbPamOverseasCountryDict")
public class TbPamOverseasCountryDictController {

	@Autowired
    public TbPamOverseasCountryDictService service;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Jun 1, 2021 8:22:26 AM
	 */
	@ApiOperation(value = "专利海外国家字典,保存/修改", notes = "专利海外国家字典,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:pam:overseas:country:dict:edit')")
	public Message save(@RequestBody TbPamOverseasCountryDict data){
    	try {
			//    		判断是否已存在
			QueryWrapper<TbPamOverseasCountryDict> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("name",data.getName());
			List<TbPamOverseasCountryDict> list = service.list(queryWrapper);
			if (list.size() > 0) {
				return Message.error(1,"国家已存在");
			}
            service.save(data);
            return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}
	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Jun 1, 2021 8:22:26 AM
	 */
	@ApiOperation(value = "专利海外国家字典删除", notes = "通过id删除专利海外国家字典信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:pam:overseas:country:dict:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}
	}


    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Jun 1, 2021 8:22:26 AM
    */
	@ApiOperation(value = "分页查询专利海外国家字典list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:pam:overseas:country:dict:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbPamOverseasCountryDict> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Jun 1, 2021 8:22:26 AM
    */
	@ApiOperation(value = "查询专利海外国家字典所有数据list", notes = "查询专利海外国家字典所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:pam:overseas:country:dict:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Jun 1, 2021 8:22:26 AM
    */
	@ApiOperation(value = "通过id获取专利海外国家字典实体类", notes = "通过id获取专利海外国家字典实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

	/**
	 * 删除
	 * @author liuqinghua
	 * @date Jun 1, 2021 8:22:26 AM
	 */
	@ApiOperation(value = "删除", notes = "删除")
	@RequestMapping(value="/deleteById",method = RequestMethod.GET)
	public Message deleteById(@RequestParam(value="id") Long id) {
		try {
            String message = service.deleteById(id);
            return Message.success(message);
		} catch (Exception e) {
			e.printStackTrace();
			return Message.error(1,"失败");
		}
	}

	   /**
	       * @Description:  修改+案件
	       * @Param:  id
	       * @return:
	       * @Author: liuqinghua
	       * @Date: 2021/6/2
	       */
    @ApiOperation(value = "修改+案件", notes = "修改+案件")
    @RequestMapping(value="/updateCountryDictById",method = RequestMethod.POST)
    public Message updateCountryDictById(@RequestBody TbPamOverseasCountryDict tbPamOverseasCountryDict) {
        try {
             service.updateCountryDictById(tbPamOverseasCountryDict);
            return Message.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(1,"失败");
        }
    }

}

